package com.huihuiyun.huihuiyun.server.controller.member.client;

import com.huihuiyun.huihuiyun.server.controller.member.client.viewobjects.HhyAdvertisementVO;
import com.huihuiyun.huihuiyun.server.domain.HhyAdvertisement;
import com.huihuiyun.huihuiyun.server.service.HhyAdvertisementService;
import com.huihuiyun.huihuiyun.server.viewobject.ResultVO;
import com.huihuiyun.huihuiyun.server.viewobject.ResultVOUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import java.util.List;
import java.util.stream.Collectors;

@Component
@Path(Const.context)
@Slf4j
public class MemberClientAdvertisementController {
    @Autowired
    private HhyAdvertisementService advertisementService;

    /*******************
     * advertisement
     ******************/
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/advertisement/findAdvertisementListByAreaCode")
    public ResultVO<HhyAdvertisementVO> findAdvertisementListByAreaCode(@QueryParam("areaCode") String areaCode) {
        try {
            List<HhyAdvertisement> hhyAdvertisementList = advertisementService.findAllByAreaCode(areaCode);
            List<HhyAdvertisementVO> advertisementVOList = hhyAdvertisementList.parallelStream().map(hhyAdvertisement -> {
                HhyAdvertisementVO hhyAdvertisementVO = new HhyAdvertisementVO();
                BeanUtils.copyProperties(hhyAdvertisement, hhyAdvertisementVO);
                return hhyAdvertisementVO;
            }).collect(Collectors.toList());
            return ResultVOUtil.success(advertisementVOList);
        } catch (Exception e) {
            log.error("", e);
            return ResultVOUtil.error(1, "获取广告失败.");
        }
    }
}